﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Activation;
using TestCenter.Platform.Apis;
using RapidWebDev.Common;
using TestCenter.Platform.DataModel;
using System.Collections.ObjectModel;
using RapidWebDev.Platform;
using AjaxControlToolkit;
using RapidWebDev.Common.Data;

namespace TestCenter.Platform.Services
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class TestCenterService : ITestCenterServices
    {
        ITestCenterApi TestCenterApi = SpringContext.Current.GetObject<ITestCenterApi>();

        IHierarchyApi hierarchyApi = SpringContext.Current.GetObject<IHierarchyApi>();

        IAuthenticationContext authenticationContext = SpringContext.Current.GetObject<IAuthenticationContext>();

        #region ITestCenterServices Members

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="testCenterHierarchyType"></param>        
        /// <returns></returns>
        public Collection<HierarchyDataObject> GetChildrenTreeNode(SearchCondition condition)
        {
            Collection<HierarchyDataObject> nodes = new Collection<HierarchyDataObject>();
            return TestCenterApi.GetChildrenTreeNode(condition.parentId, condition.testCenterHierarchyType, nodes);
        }


        public Collection<HierarchyDataObject> GetTestCaseSuitesInTestPlan(string projectId, string planId) 
        {
            Collection<HierarchyDataObject> nodes = new Collection<HierarchyDataObject>();
            using (TestCenterDataContext ctx = DataContextFactory.Create<TestCenterDataContext>())
            {
                var q = ctx.Testplan_Testcases.Where(x => x.TestPlanId.ToString() == planId).Select(y=>y.TestSuiteId.ToString());

                IEnumerable<HierarchyDataObject> suites = hierarchyApi.GetAllChildren("1", new Guid(projectId));

                return new Collection<HierarchyDataObject>(suites.Where(x => q.ToArray<string>().Contains(x.Code)).ToList());
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="testCenterHierarchyType"></param>
        /// <returns></returns>
        public Collection<HierarchyDataObject> GetAllTestSuiteUnderTestProject(string parentId)
        {
            Collection<HierarchyDataObject> ret = new Collection<HierarchyDataObject>();
            //IEnumerable<HierarchyDataObject> results = TestCenterApi.GetChildrenTreeNode(
            //foreach (HierarchyDataObject tmp in results)
            //{
            //    ret.Add(tmp);
            //}
            return ret;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        public string SaveTestSuite(TestSuitePost post)
        {
            TestSuite entity = new TestSuite()
            {
                Name = post.Name,
                Summary = post.Summary,
                CreatedBy = authenticationContext.User.UserId,
                CreatedOn = DateTime.Now
            };

            return TestCenterApi.SaveTestSuite(entity, post.ParentId);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ProjectId"></param>
        /// <returns></returns>
        public Collection<TestPlanJson> GetTestPlans()
        {
            Collection<TestPlanJson> rets = new Collection<TestPlanJson>();
            IEnumerable<HierarchyDataObject> plans = null;
            //get all testplan
            //if (ProjectId.Equals("null"))
            plans = hierarchyApi.GetAllChildren("3", new Guid(authenticationContext.Session["CurrentProject"].ToString()));
            //else
            //    plans = hierarchyApi.GetAllChildren("3", new Guid(ProjectId));
            foreach (var tmp in plans)
            {
                TestPlanJson tp = new TestPlanJson() { TestPlanId = tmp.HierarchyDataId,Name = tmp.Name};
                rets.Add(tp);
            }
            return rets;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PlanId"></param>
        /// <returns></returns>
        public Collection<HierarchyDataObject> GetBuilds(string PlanId) 
        {
            Collection<HierarchyDataObject> rets = new Collection<HierarchyDataObject>();
            //get all testplan
            IEnumerable<HierarchyDataObject> builds = hierarchyApi.GetAllChildren("4", new Guid(PlanId));
           foreach (var tmp in builds)
                rets.Add(tmp);
            return rets;
        }
        #endregion
    }
}
